package com.yc.cloud.activiti.utils;

import cn.hutool.core.util.ObjectUtil;
import org.activiti.engine.*;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 杨智杰
 * @since 2021/9/18 14:11
 */
@Component
public class ActivitiUtil {

    /**
     * 节点相关
     */
    @Resource
    protected RuntimeService runtimeService;

    /**
     * 任务相关
     */
    @Resource
    protected TaskService taskService;

    /**
     * 流程相关
     */
    @Resource
    protected ProcessEngine processEngine;

    /**
     * 资源相关
     */
    @Resource
    protected RepositoryService repositoryService;

    /**
     * 历史相关
     */
    @Resource
    protected HistoryService historyService;

    /**
     * 部署一个简单的流程示例
     */
    public void deployAProcess() {
        repositoryService.createDeployment()
                .addClasspathResource("process/请假流程.bpmn20.xml")
                .addClasspathResource("process/请假流程.png")
                .name("请假流程")
                .deploy();
    }

    /**
     * 启动实例带流程变量
     */
    public ProcessInstance start(String key, Map<String, Object> variables) {
        ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key, variables);
        return startProcessInstanceByKey;
    }

    /**
     * 启动实例带流程变量
     */
    public ProcessInstance start(String key) {
        ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key);
        return startProcessInstanceByKey;
    }

    /**
     * 推进任务 带流程变量 方便流转到下以个节点
     */
    public void completeTask(String taskId, Map<String, Object> variables) {
        taskService.complete(taskId, variables);
    }

    /**
     * 推进任务
     */
    public void completeTask(String taskId) {
        taskService.complete(taskId);
    }

    /**
     * 推进任务 实例id 和 人的名称
     */
    public void completeTaskByPIdAndName(String pid, String name) {
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        Task task = createTaskQuery.processInstanceId(pid).taskAssignee(name).singleResult();
        taskService.complete(task.getId());
    }

    /**
     * 推进任务 实例id 和 人的名称
     */
    public void completeTaskByPIdAndName(String pid, String name, Map<String, Object> variables) {
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        Task task = createTaskQuery.processInstanceId(pid).taskAssignee(name).singleResult();
        taskService.complete(task.getId(), variables);
    }

    /**
     * 查询某个人的任务列表
     */
    public List<Task> queryTaskList(String name) {
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        createTaskQuery.taskAssignee(name);
        List<Task> list = createTaskQuery.list();
        return list;
    }

    /**
     * 查询某个人的当前任务
     */
    public Task queryTaskByName(String pid, String name) {
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        createTaskQuery.taskAssignee(name);
        createTaskQuery.processInstanceId(pid);
        Task task = createTaskQuery.singleResult();
        return task;
    }

    /**
     * 查询所有任务
     *
     * @return
     */
    public List<Task> queryAllTask(){
        List<Task> list = taskService.createTaskQuery().list();
        return taskService.createTaskQuery().list();
    }

    /**
     * 查询当前执行对象(当前活动节点)
     */
    public Execution queryExecution(String pid) {
        ExecutionQuery createExecutionQuery = runtimeService.createExecutionQuery();
        Execution execution = createExecutionQuery.processInstanceId(pid).singleResult();
        return execution;
    }

    /**
     * 流程是否结束
     *
     * @param pid
     * @return
     */
    public boolean isEnd(String pid) {
        ProcessInstanceQuery createProcessInstanceQuery = runtimeService.createProcessInstanceQuery();
        ProcessInstance singleResult = createProcessInstanceQuery.processInstanceId(pid).singleResult();
        return ObjectUtil.isNull(singleResult);
    }
}
